<?php
namespace app\common\model\mysql;

// use think\Model;
class Category extends ModelBase{
	// 指定数据库表
	public $table = 'mall_category';

	// 获取正常分类
	public function getNormalCategorys($field = "*"){
		$where = [
			'status' => config('status.mysql.normal'),
		];
		$order = [
			'listorder' => 'desc',
			'id' => 'desc',
		];

		$result = $this->where($where)
			->field($field)
			->order($order)
			->select();
		//echo $this->getLastSql();exit;
		return $result;
	}

	public function getNormalCategorys2($where = [],$field = "*"){
		$data = [
			'status' => config('status.mysql.normal'),
		];
		
		$order = [
			'listorder' => 'desc',
			'id' => 'desc',
		];

		$result = $this->where($where)
			->where($data)
			->field($field)
			->order($order)
			->select();
		//echo $this->getLastSql();exit;
		return $result;
	}

	//获取正常一级分类
	public function getNormalByPid($pid = 0,$field){
		$where = [
			'pid' => $pid,
			'status' => config('status.mysql.normal'),
		];
		$order = [
			'listorder' => 'desc',
			'id' => 'desc',
		];

		$res = $this->where($where)
			->field($field)
			->order($order)
			->select();
		return $res;
	}

    // 获取分类列表
	public function getLists($where,$num = 10){
		$order = [
			'listorder' => 'desc',
			'id' => 'desc',
		];

		$result = $this->where('status','<>',config('status.mysql.delete'))
			->where($where)
			->order($order)
			->paginate($num);

		// echo $this->getLastSql();exit;
		return $result;
	}

	/*
	// 根据ID更新数据
	public function updateById($id,$data){
		$data['update_time'] = time();
		return $this->where(['id'=>$id])->save($data);
	}
	*/

	public function getChildCountInPids($condition){
		$where[] = ["pid","in",$condition['pid']];
		$where[] = ["status","<>",config("status.mysql.delete")];
		$res = $this->where($where)
					->field(["pid","count(*) as count"])
					->group("pid")
					->select();
		//echo $this->getLastSql();exit;
		return $res;
	}
}